#pragma once
#include<stdio.h>
#include<stdlib.h>
#define HASH_TABLE_NUM 10

typedef struct List {
	char data;
	List* next;		//数据使用链表存储
}List;

/*
* 链地址解决冲突的哈希表
*/
typedef struct HashTableLineSite {
	List** ListData;
	int num;
}HashLink;

HashLink* InitHashLink() {
	HashLink* hashLink = (HashLink*)malloc(sizeof(HashLink));
	hashLink->ListData = (List**)malloc(sizeof(List) * HASH_TABLE_NUM);

	for (int i = 0; i < HASH_TABLE_NUM; i ++) {
		hashLink->ListData[i] = (List*)malloc(sizeof(List));
		hashLink->ListData[i]->data = 0;
		hashLink->ListData[i]->next = NULL;
	}

	hashLink->num = 0;
	return hashLink;
}

int HashFuncLink(char data) {
	return data % HASH_TABLE_NUM;
}

void HashLinkPut(HashLink* hash, char data) {
	List* lNode = (List*)malloc(sizeof(List));
	lNode->data = data;
	lNode->next = NULL;

	int index = HashFuncLink(data);
	List* pList = hash->ListData[index];
	while (pList->next != NULL) {
		pList = pList->next;
	}
	pList->next = lNode;
}

void HashLinkPrint(HashLink* hash) {
	for (int i = 0; i < HASH_TABLE_NUM; i ++) {
		printf("%d", i);
		List* pList = hash->ListData[i];
		while (pList->next != NULL) {
			printf("%c -> ", pList->data);
			pList = pList->next;
		}
		printf("%c -> ", pList->data);
		printf("NULL\n");
	}
}

int FindElementHashLink(HashLink* hash, char data) {
	int index = HashFuncLink(data);
	List* pList = hash->ListData[index];
	int count = 1;
	int data_index = 0;
	while (pList->data != data) {
		if (pList->next == NULL) {
			return -1;
		}
		pList = pList->next;
		count++;
		data_index++;
	}
	printf("在索引为 %d 的位置, 比较了 %d 次\t", index, count);
	return data_index;
}